const express = require('express')
const c = require('../connection')
const router = express.Router()
//客户信息列表
router.get('/list', (req, res) => {
    pageNum = req.query.pageNum * 1 || 1
    pageCount = 99999
    start = (pageNum - 1) * pageCount
    c.query(`select count(cid) as count from uc_customer`, (err, r) => {
        if (err) {
            throw err
        }
        if (r.length > 0) {
            total = r[0].count
            c.query(`select * from uc_customer limit ?,?`, [start, pageCount], (err, r) => {
                if (err) {
                    throw err
                }
                if (r.length > 0) {
                    res.send({
                        code: 200,
                        msg: '客户列表查询成功',
                        data: r,
                        total: total,
                        pageNum: pageNum,
                        pageCount: pageCount,
                    })
                } else {
                    res.send({
                        code: 401,
                        msg: '客户列表查询失败',
                    })
                }
            })
        }
    })
})
//通过cid查询客户详细信息
router.get('/query', (req, res) => {
    var obj = req.query
    c.query(`select * from uc_customer where cid=?`, [obj.cid], (err, r) => {
        if (err) {
            throw err
        }
        if (r.length > 0) {
            res.send({
                code: 200,
                msg: '查询客户信息成功',
                data: r
            })
        } else {
            res.send({
                code: 200,
                msg: '查询客户信息失败',
            })
        }
    })
})
//客户所属行业
router.get('/industry', (req, res) => {
    c.query(`select * from uc_industry`, (err, r) => {
        if (err) {
            throw err
        }
        if (r.length > 0) {
            res.send({
                code: 200,
                msg: '行业列表查询成功',
                data: r
            })
        } else {
            res.send({
                code: 200,
                msg: '行业列表查询失败',
            })
        }
    })
})
//查询销售人员
router.get('/saleperson', (req, res) => {
    c.query(`select * from uc_saleperson`, (err, r) => {
        if (err) {
            throw err
        }
        if (r.length > 0) {
            res.send({
                code: 200,
                msg: '销售人员列表查询成功',
                data: r
            })
        } else {
            res.send({
                code: 200,
                msg: '销售人员列表查询查询失败',
            })
        }
    })
})
//查询付款状态
router.get('/status', (req, res) => {
    c.query(`select * from uc_status`, (err, r) => {
        if (err) {
            throw err
        }
        if (r.length > 0) {
            res.send({
                code: 200,
                msg: '状态列表查询成功',
                data: r
            })
        } else {
            res.send({
                code: 200,
                msg: '状态列表查询失败',
            })
        }
    })
})
//查询客户来源列表
router.get('/source', (req, res) => {
    c.query(`select * from uc_source`, (err, r) => {
        if (err) {
            throw err
        }
        if (r.length > 0) {
            res.send({
                code: 200,
                msg: '客户来源查询成功',
                data: r
            })
        } else {
            res.send({
                code: 200,
                msg: '客户来源查询失败',
            })
        }
    })
})
//查询性别列表
router.get('/gender', (req, res) => {
    c.query(`select * from uc_gender`, (err, r) => {
        if (err) {
            throw err
        }
        if (r.length > 0) {
            res.send({
                code: 200,
                msg: '性别查询成功',
                data: r
            })
        } else {
            res.send({
                code: 200,
                msg: '性别查询失败',
            })
        }
    })
})
//修改客户信息
router.post('/update', (req, res) => {
    var obj = req.body
    c.query(`update uc_customer set ? where cid=?`, [obj, obj.cid], (err, r) => {
        if (err) {
            throw err
        }
        if (r.changedRows === 0) {
            res.send({
                code: 401,
                msg: '修改失败'
            })
        } else {
            res.send({
                code: 200,
                msg: '修改成功',
                data: r
            })
        }
    })
})
//删除客户信息
router.post('/delete', (req, res) => {
    var obj = req.body
    c.query(`delete from uc_customer where cid=?`, [obj.cid], (err, r) => {
        if (err) {
            throw err
        }
        if (r.affectedRows === 1) {
            res.send({
                code: 200,
                msg: '删除成功',
                data: r
            })
        } else {
            res.send({
                code: 401,
                msg: '删除失败',
                data: r
            })
        }
    })
})
//新增客户信息
router.post('/add', (req, res) => {
    var obj = req.body
    c.query(`insert into uc_customer set ?`, [obj], (err, r) => {
        if (err) {
            throw err
        }
        res.send({
            code: 200,
            msg: '新增客户信息成功',
            data: r
        })
    })
})
module.exports = router
